package analyzer.protocal;

import analyzer.Common;

import java.io.FileInputStream;
import java.io.IOException;

public class TCP {
    public static void analyze(FileInputStream in)
    {

    }
    private  static void srcPort(FileInputStream in)
    {
        byte[] b = new byte[2];
        try {
            in.read(b);
            int srcPort = b[1]&0xff;
            srcPort += 256 * (b[0]&0xff);
            System.out.println("source port: " + srcPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private  static void destPort(FileInputStream in)
    {
        byte[] b = new byte[2];
        try {
            in.read(b);
            int destPort = b[1]&0xff;
            destPort += 256 * (b[0]&0xff);
            System.out.println("destination port: " + destPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void sequenceNumber(FileInputStream in)
    {
        byte[] bytes = new byte[4];
        try {
            int num = 0;
            in.read(bytes);
            num += bytes[3]&0xff;
            num += 256 * (bytes[2]&0xff);
            num += 256 * 256 * (bytes[1]&0xff);
            num +=256 * 256 * 256 * (bytes[0]&0xff);
            System.out.println("sequence number is " + num);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void acknowledgeNumber(FileInputStream in)
    {
        byte[] bytes = new byte[4];
        try {
            int num = 0;
            in.read(bytes);
            num += bytes[3]&0xff;
            num += 256 * (bytes[2]&0xff);
            num += 256 * 256 * (bytes[1]&0xff);
            num +=256 * 256 * 256 * (bytes[0]&0xff);
            System.out.println("sequence number is " + num);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void headerLength(FileInputStream in)
    {
        byte[] b = new byte[1];
        try {
            in.read(b);
            int len = b[0] >> 4;
            System.out.println("Header length of tcp packet : " + len);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void flags(FileInputStream in)
    {
        byte[] b = new byte[1];
        try {
            in.read(b);
            boolean cwr = false;
            if ((b[0] & 0x80) != 0)
            {
                cwr = true;
            }
            System.out.println("congestion window reduce flag is: " + cwr);
            boolean ece = false;
            if ((b[0] & 0x40) != 0)
            {
                ece = true;
            }
            System.out.println("ECN Echo flag is: " + ece);
            boolean urg = false;
            if ((b[0] & 0x20) != 0)
            {
                urg = true;
            }
            System.out.println("Urgent Flag is: " + urg);
            boolean ack = false;
            if ((b[0] & 0x10) != 0)
            {
                ack = true;
            }
            System.out.println("ACK Flag is: " + ack);
            boolean psh = false;
            if ((b[0] & 0x08) != 0)
            {
                psh = true;
            }
            System.out.println("Push flag is: " + psh);
            boolean rst = false;
            if ((b[0] & 0x04) != 0)
            {
                rst = true;
            }
            System.out.println("reset flag is: " + rst);
            boolean syn = false;
            if ((b[0] & 0x02) != 0)
            {
                syn = true;
            }
            System.out.println("Syn flag is: " + syn);
            boolean fin = false;
            if ((b[0] & 0x01) != 0)
            {
                fin = true;
            }
            System.out.println("Fin flag is: " + fin);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private static void windowSize(FileInputStream in)
    {
        byte[] b = new byte[2];
        try {
            in.read(b);
            int size = b[1]&0xff;
            size += 256 * (b[0]&0xff);
            System.out.println("Window size : " + size);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void checkSum(FileInputStream in)
    {
        byte[] b = new byte[2];
        try {
            in.read(b);
            String checksum = "0x" + (b[0]&0xff) + (b[1]&0xff);
            System.out.println("TCP Checksum : " + checksum);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void identification(FileInputStream in)
    {
        byte[] b = new byte[2];
        try {
            in.read(b);
            String urgentPointer = "0x" + (b[0]&0xff) + (b[1]&0xff);
            System.out.println("Urgent Pointer: " + urgentPointer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
